package cn.wolfcode.trip.app.controller;


import cn.wolfcode.trip.base.domain.*;
import cn.wolfcode.trip.base.query.CompanionQueryObject;
import cn.wolfcode.trip.base.query.TravelQuery;
import cn.wolfcode.trip.base.service.*;
import cn.wolfcode.trip.base.domain.Companion;
import cn.wolfcode.trip.base.domain.StrategyComment;
import cn.wolfcode.trip.base.domain.Travel;
import cn.wolfcode.trip.base.domain.User;
import cn.wolfcode.trip.base.query.*;
import cn.wolfcode.trip.base.service.*;
import cn.wolfcode.trip.base.util.JSONResult;
import cn.wolfcode.trip.base.util.UserContext;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.xml.stream.events.Comment;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 注册用户资源控制器
 */
@RestController
@RequestMapping("/users")
public class UserController {

    @Autowired
    private IUserService userService;
    @Autowired
    private ITravelService travelService;
    @Autowired
    private ICompanionService companionService;
    @Autowired
    private IPraiseService praiseService;
    @Autowired
    private IShoppingCartService shoppingCartService;
    @Autowired
    private IPrivateMessageService messageService;

    @Autowired
    private IStrategyCommentService commentService;
    @Autowired
    private ISystemNotificationService systemNotificationService;

    @Autowired
    private IFriendshipReplyService friendshipReplyService;
    @Autowired
    private IFriendshipCommentService friendshipCommentService;
    @Autowired
    private IStrategyUserCommentService strategyCommentService;
    @Autowired
    private IStrategyReplyUserCommentSerivce strategyReplyCommentSerivce;

    @Autowired
    private IAddService addService;

    @Autowired
    private IProductOrderService productOrderService;



    @PutMapping("/{productOrderId}/productOrder")
    public void updateState(@PathVariable Long productOrderId,Integer freight){
        productOrderService.updateState(productOrderId,freight);
    }



    /**
     * lituan
     * 根据条件查出订单
     * @return
     */
    @GetMapping("/productOrders")
    public PageInfo<ProductOrder> queryProductOrder(ProductOrderQuery qo){
        qo.setPageSize(0);
       PageInfo<ProductOrder> query = productOrderService.queryProductOrder(qo);

        return query;
    }

    /**
     * lituan
     * 查询所有未读的评论/回复数量
     * @return
     */

    @GetMapping("/beCommentCounts")
    public int selectTotalCountForUnreadComment(){
        int fc =  friendshipCommentService.selectTotalCountForUnreadComment();
        int fr = friendshipReplyService.selectTotalCountForUnreadComment();
        int sc =strategyCommentService.selectTotalCountForUnreadComment();
        int sr =strategyReplyCommentSerivce.selectTotalCountForUnreadComment();
        return fc+fr+sc+sr;
    }

    /**
     * 查询所有未读的朋友圈评论/回复数量
     * @return
     */
    @GetMapping("/beFriendshipCommentCounts")
    public int selectTotalCountForFriendshipComment(){
        int fc =  friendshipCommentService.selectTotalCountForUnreadComment();
        int fr = friendshipReplyService.selectTotalCountForUnreadComment();
        return fc+fr;
    }

    /**
     * 查询所有未读的点评评论/回复数量
     * @return
     */
    @GetMapping("/beStategyCommentCounts")
    public int selectTotalCountForStategyComment(){
        int sc =strategyCommentService.selectTotalCountForUnreadComment();
        int sr =strategyReplyCommentSerivce.selectTotalCountForUnreadComment();
        return sc+sr;
    }

    /**
     * 查询所有未读的点评评论
     */
    @GetMapping("/beStrategyComments")
    public List selectForStategyComment(){
        List<StrategyComment> strategyComments =strategyCommentService.selectComment();
        return strategyComments;
    }

    /**
     * 查询所有未读的点评回复
     */
    @GetMapping("/beStrategyReplys")
    public List<StrategyReplyUserComment> selectForStategyReply(){
        return strategyReplyCommentSerivce.selectReply();
    }

    /**
     * 将指定id的评论改为已读
     * @param strategyCommentId
     */
    @PutMapping("/beStrategyComments")
    public void updateRead(Long strategyCommentId){
        strategyCommentService.updateRead(strategyCommentId);

    }

    /**
     * 将指定id的回复改为已读
     * @param
     */
    @PutMapping("/beStartegyReplies")
    public void updateStrategyComment(Long replyId){
        strategyReplyCommentSerivce.updateRead(replyId);
    }

    /**
     * 列出朋友圈未读评论
     * @return
     */
    @GetMapping("/beFriendshipComments")
    public List<FriendshipComment> selectForFriendshipComment(){
        return friendshipCommentService.selectForComment();
    }

    /**
     * 列出朋友圈未读回复
     * @return
     */
    @GetMapping("/beFirendshipReplies")
    public List<FriendshipReply> selectForFriendshipReply(){
        return friendshipReplyService.selectReply();
    }

    /**
     * 改变指定评论为已读
     * @param friendshipCommentId
     */
    @PutMapping("/beFriendshipComments")
    public void updataRead(Long friendshipCommentId){
        friendshipCommentService.updateRead(friendshipCommentId);
    }

    /**
     * 改变指定回复为已读
     * @param friendshipReplyId
     */
    @PutMapping("/beFriendshipReply")
    public void updataReplyRead(Long friendshipReplyId){
        friendshipReplyService.updateRead(friendshipReplyId);
    }



    /**不要动wqwq
     * 获取指定用户的所有结伴
     * @param qo
     * @return
     */
    @GetMapping("{authorId}/companions")
    public PageInfo queryCompanions(CompanionQueryObject qo){

        qo.setOrderBy("c.createTime desc");
        PageInfo<Companion> list = companionService.query(qo);
        return  list;
    }


    /**
     * 查询所有用户
     * @return
     */
    @GetMapping
    public List<User> list(){
       return userService.selectAll();
    }


    /**
     *
     * @return
     */
    @GetMapping("private")
    public Map<String,Object> listPrivateUser(){
        return messageService.selectlistPrivateUser();
    }


    /**
     * 注册(保存)  传入一个user
     * 后台根据邮箱判断用户是否存在,存在就抛出异常
     * @param user
     * @return
     */
    @PostMapping
    public Object registe(User user){
        JSONResult jsonResult = new JSONResult();

        try {
            userService.saveOrUpdate(user);
        } catch (Exception e) {
            jsonResult.mark(e.getMessage());
            e.printStackTrace();
        }

        return jsonResult;
    }

    /**
     * 根据用户id去保存或修改用户信息
     * @param user
     * @return
     */
    @PutMapping("{id}")
    public Object saveOrUpdate(User user){
        JSONResult jsonResult= new JSONResult();

        try {
            userService.saveOrUpdate(user);
            jsonResult.setUser(user);
        } catch (Exception e) {
            jsonResult.mark("保存失败,请稍后再试");
            e.printStackTrace();
        }

        return jsonResult;
    }

    //获取指定用户的所有游记
    @GetMapping("{authorId}/travels")
    public PageInfo queryTravel(TravelQuery qo){

        qo.setOrderBy("t.lastUpdateTime desc");
        List<Travel> list=travelService.query(qo);

        return  new PageInfo(list);
    }

    /**qiuyanquan
     * 获取指定用户的所有点评
     */
    @GetMapping("{authorId}/comments")
    public PageInfo queryComment(CommentQuery qo){
        //qo.setOrderBy("t.lastUpdateTime desc");
        List<StrategyComment> list=commentService.queryComment(qo);
        return  new PageInfo(list);
    }

    /**
     * 根据用户id获取用户
     * @param id
     * @return
     */
    @GetMapping("{id}")
    public User getUserByParamKey(@PathVariable Long id){
        System.out.println(11);
        return userService.getUserByParamKey(id);
    }


    /**
     *wqwqwqw
     * 不要动
     * @param companionId
     * @return
     */
    @GetMapping("companion")
    public Boolean getUserByCompanionId(Long companionId){
        System.out.println(55);
        Boolean aBoolean = userService.selectByCompanionId(companionId);
        System.out.println(aBoolean);
        return aBoolean;
    }

    /** tiger
     * 获取购物车中商品
     * @param userId
     * @return
     */
    @GetMapping("{userId}/shoppingCarts")
    public PageInfo getShoppingCartByUserId(@PathVariable Long userId){
        List<ShoppingCart> list=shoppingCartService.getShoppingCartByUserId(userId);
        return new PageInfo<>(list);
    }

    @GetMapping("selects")
    public List<User> selectUser(String str){
        List<User> list = userService.selectByEmailAndNickName(str);
        return list;
    }


    /**qiuyanquan
     * 首次访问页面显示是否关注
     * @param
     * @return
     */
    @GetMapping("/isAttention")
    public JSONResult isAttention(AttentionQueryObject qo) {
        JSONResult json = new JSONResult();
        Integer i = userService.selectAttention(qo);
        if (i > 0) {
            return json;
        } else {
            json.mark("没有关注");
            return json;
        }
    }

    /**
     * 修改用户关注状态
     *
     * @param
     * @return
     */
    @PostMapping("/attention")
    public Map changeattention(AttentionQueryObject qo) {
        int i = userService.changeAttention(qo.getAttentionuserId(),qo.getBeattentionuserId());

        HashMap map = new HashMap();
        User user = userService.getUserByParamKey(qo.getBeattentionuserId());
        map.put("fans", user.getFans());//粉丝
        map.put("attention", user.getAttention());//关注
        if (i > 0) {
            map.put("success", true);
        } else {
            map.put("success", false);
        }
        return map;
    }

    /**qyq
     * 查询单个用户的系统通知信息
     * @param
     * @return
     */
    @GetMapping("/systemNotification")
    public PageInfo getsystemNotification(QueryObject qo) {
        return systemNotificationService.listAll(qo);
    }


    @GetMapping("/systemNotificationNum")
    public int getsystemNotificationNum(QueryObject qo) {
        return systemNotificationService.getsystemNotificationNum(qo);
    }

    @PostMapping("/updateType")
    public Object updateType(QueryObject qo,int type){
        systemNotificationService.updateType(qo,type);
        return new JSONResult();
    }



    /**kk
     * 更新或保存购物车
     * @param shoppingCart
     * @return
     */

    @PostMapping("cart")
    public Object saveOrUpdate(ShoppingCart shoppingCart){
        JSONResult jsonResult= new JSONResult();
        try {
            shoppingCartService.saveOrUpdate(shoppingCart);
        } catch (Exception e) {
            jsonResult.mark("保存失败,请稍后再试");
            e.printStackTrace();
        }
        return jsonResult;
    }


    /**
     * 保存到购物车返回一个此id
     *
     */
    @PostMapping("cartSave")
    public ShoppingCart saveAndReutenId(ShoppingCart carInfo){
        ShoppingCart cart = shoppingCartService.saveAndReutenId(carInfo);
        return  cart;
    }









    /** qyq
     * 查询用户拥有的所有游记
     * @param userId
     * @return
     */
    @GetMapping("/travelNum")
    public int getTravelNum(Long userId){
        return travelService.getTravelNum(userId);
    }

    @GetMapping("/fans")
    public int getFansNum(Long userId){
        return userService.getFansNum(userId);
    }
    /**
     * 更新游记/攻略点评的点赞状态 已读未读
     * @param
     * @return
     */
    @PutMapping("/updatePraiseState")
    public Object updatePraiseState(Long praiseId){
        praiseService.updatePraiseState(praiseId);
        return new JSONResult();
    }

    /**
     * 更新朋友圈的点赞状态 已读未读
     * @param praiseId
     * @return
     */
    @PutMapping("/updateFriendshipPraiseState")
    public Object updateFriendshipPraiseState(Long praiseId){
        praiseService.updateFriendshipPraiseState(praiseId);
        return new JSONResult();
    }
    /** tiger
     * 获取用户的收货地址
     * @param userId
     * @return
     */
    @GetMapping("{userId}/adds")
    public PageInfo getAddsByUserId(@PathVariable Long userId){
        List list = addService.getAddsByUserId(userId);
        return new PageInfo<>(list);
    }

    @GetMapping("/praiseNum")
    public Object getPraiseNum(){
        return praiseService.getPraiseNumbers(UserContext.getCurrentUser().getId());
    }


    @GetMapping("/praises")
    public Object getPraises(Integer type){
        return praiseService.getPraise(UserContext.getCurrentUser().getId(),type);
    }
}
